<html><head><title>BindEvent.pb</title></head>
<body bgcolor="#EFF1F0" link="#3A3966" vlink="#000000" alink="#000000">
<pre><font face="Courier New, Courier, mono"size="2"><font color="#3A3966">;
</font><font color="#3A3966">; ------------------------------------------------------------
</font><font color="#3A3966">;
</font><font color="#3A3966">;   PureBasic - BindEvent and  BindGadgetEvent example file
</font><font color="#3A3966">;
</font><font color="#3A3966">;    (c) Fantaisie Software
</font><font color="#3A3966">;
</font><font color="#3A3966">; ------------------------------------------------------------
</font><font color="#3A3966">;
</font>
<font color="#3A3966">; Let's create a speedButton which produce event as long as it's pressed.
</font>
<b><font color="#3A3966">Global</font></b> Gadget, Pressed.b, n
<b><font color="#3A3966">Global</font></b> Min=-50, Max=50

<font color="#3A3966">; Change the speed here !
</font><b><font color="#3A3966">Global</font></b> Delay=50 <font color="#3A3966">; 50 ms
</font>

<b><font color="#3A3966">Procedure</font></b> <font color="#3A3966">paint</font>(canvas, state, text$)
  <font color="#3A3966">; Paint canvas as fake button &quot;+&quot; and fake button &quot;-&quot;
</font>  <b><font color="#3A3966">Protected</font></b> w, h
  <font color="#3A3966">;State: 0     = Pushed
</font>  <font color="#3A3966">;State: Not 0 = Released
</font>  <b><font color="#3A3966">If</font></b> <font color="#3A3966">StartDrawing</font>(<font color="#3A3966">CanvasOutput</font>(canvas))
<font color="#3A3966">    Box</font>(0,0,31,31,$CFFFFC) <font color="#3A3966">; Yellow background
</font>    <b><font color="#3A3966">If</font></b> state=0 <font color="#3A3966">; UP
</font><font color="#3A3966">      LineXY</font>(0,31,31,31,$000000)
<font color="#3A3966">      LineXY</font>(31,0,31,31,$000000)
<font color="#3A3966">      LineXY</font>(0,0,31,0,$FFFFFF)
<font color="#3A3966">      LineXY</font>(0,0,0,31,$FFFFFF)
    <b><font color="#3A3966">Else</font></b>       <font color="#3A3966">;DOWN
</font><font color="#3A3966">      LineXY</font>(0,31,31,31,$FFFFFF)
<font color="#3A3966">      LineXY</font>(31,0,31,31,$FFFFFF)
<font color="#3A3966">      LineXY</font>(0,0,31,0,$000000)
<font color="#3A3966">      LineXY</font>(0,0,0,31,$000000)
    <b><font color="#3A3966">EndIf</font></b>
    w=<font color="#3A3966">TextWidth</font>(text$)
    h=<font color="#3A3966">TextHeight</font>(text$)
<font color="#3A3966">    DrawText</font>(16-w/2, 16-h/2, Text$, $000000, $CFFFFC)
    
<font color="#3A3966">    StopDrawing</font>()
  <b><font color="#3A3966">EndIf</font></b>
  
<b><font color="#3A3966">EndProcedure</font></b>

<b><font color="#3A3966">Procedure</font></b> <font color="#3A3966">OnSpeedButtonEvent</font>()
  <font color="#3A3966">; Bind all button &quot;-&quot; events and all button &quot;+&quot; events
</font>  Gadget =<font color="#3A3966"> EventGadget</font>()
  
  <b><font color="#3A3966">Select</font></b> <font color="#3A3966">EventType</font>() <font color="#3A3966">; Which event ?
</font>    <b><font color="#3A3966">Case</font></b> <font color="#924B72">#PB_EventType_MouseEnter</font>
      <b><font color="#3A3966">If</font></b> Gadget=0
        <b><font color="#3A3966">Debug</font></b> &quot;Enter button -&quot;
      <b><font color="#3A3966">ElseIf</font></b> Gadget=1
        <b><font color="#3A3966">Debug</font></b> &quot;Enter button +&quot; 
      <b><font color="#3A3966">EndIf</font></b>      
      
    <b><font color="#3A3966">Case</font></b> <font color="#924B72">#PB_EventType_MouseLeave</font>
      <b><font color="#3A3966">If</font></b> Gadget=0
        <b><font color="#3A3966">Debug</font></b> &quot;Leave button -&quot;
      <b><font color="#3A3966">ElseIf</font></b> Gadget=1
        <b><font color="#3A3966">Debug</font></b> &quot;Leave button +&quot; 
      <b><font color="#3A3966">EndIf</font></b>  
      
    <font color="#3A3966">; Each time a button is left clicked  
</font>    <b><font color="#3A3966">Case</font></b> <font color="#924B72">#PB_EventType_LeftButtonDown</font>
      Pressed = <font color="#924B72">#True</font>
      
      <b><font color="#3A3966">Select</font></b> Gadget
        <b><font color="#3A3966">Case</font></b> 0 <font color="#3A3966">; Button &quot;-&quot;
</font><font color="#3A3966">          paint</font>(0,1,&quot;-&quot;) <font color="#3A3966">; Paint the button &quot;-&quot; pushed
</font>        <b><font color="#3A3966">Case</font></b> 1 <font color="#3A3966">; Button &quot;+&quot;
</font><font color="#3A3966">          paint</font>(1,1,&quot;+&quot;) <font color="#3A3966">; Paint the button &quot;+&quot; pushed
</font>      <b><font color="#3A3966">EndSelect</font></b>
      
    <b><font color="#3A3966">Case</font></b> <font color="#924B72">#PB_EventType_LeftButtonUp</font>
      Pressed = <font color="#924B72">#False</font>
<font color="#3A3966">      paint</font>(0,0,&quot;-&quot;)     <font color="#3A3966">; Paint the button &quot;-&quot; released
</font><font color="#3A3966">      paint</font>(1,0,&quot;+&quot;)     <font color="#3A3966">; Paint the button &quot;+&quot; released
</font>      
  <b><font color="#3A3966">EndSelect</font></b>
<b><font color="#3A3966">EndProcedure</font></b>

<b><font color="#3A3966">Procedure</font></b> <font color="#3A3966">OnTimer</font>()
  <b><font color="#3A3966">If</font></b> Pressed <font color="#3A3966">; Each time a button is left clicked and as long as it's clicked
</font>    <b><font color="#3A3966">Select</font></b> Gadget
      <b><font color="#3A3966">Case</font></b> 0 <font color="#3A3966">; Button &quot;-&quot;  
</font>        <b><font color="#3A3966">If</font></b> n&gt;Min
         n-1 
<font color="#3A3966">         SetGadgetState</font>(2,<font color="#3A3966"> GetGadgetState</font>(2)-1) <font color="#3A3966">; ProgressBar down        
</font>        <b><font color="#3A3966">EndIf</font></b>
        
      <b><font color="#3A3966">Case</font></b> 1 <font color="#3A3966">; Button &quot;+&quot;      
</font>        <b><font color="#3A3966">If</font></b> n&lt;Max
         n+1 
<font color="#3A3966">         SetGadgetState</font>(2,<font color="#3A3966"> GetGadgetState</font>(2)+1) <font color="#3A3966">; ProgressBar up	
</font>        <b><font color="#3A3966">EndIf</font></b>
    <b><font color="#3A3966">EndSelect</font></b>    
    
    <b><font color="#3A3966">Debug</font></b> n
  <b><font color="#3A3966">EndIf</font></b>
<b><font color="#3A3966">EndProcedure</font></b>

<font color="#3A3966">; Open a window with some gadgets
</font><font color="#3A3966">OpenWindow</font>(0, 0, 0, 400, 100, &quot;Clic a looooong time please on +/- ;)&quot;, <font color="#924B72">#PB_Window_SystemMenu</font> | <font color="#924B72">#PB_Window_ScreenCentered</font>)
<font color="#3A3966">CanvasGadget</font>(0, 10, 30, 32, 32)        <font color="#3A3966">; Fake button -
</font><font color="#3A3966">CanvasGadget</font>(1, 400-32-10, 30, 32, 32) <font color="#3A3966">; Fake button +
</font><font color="#3A3966">paint</font>(0,0,&quot;-&quot;)
<font color="#3A3966">paint</font>(1,0,&quot;+&quot;)
<font color="#3A3966">ProgressBarGadget</font>(2,52,30,296,32,Min,Max,<font color="#924B72">#PB_ProgressBar_Smooth</font>)

<font color="#3A3966">AddWindowTimer</font>(0, 100, Delay) <font color="#3A3966">; Timer 100 ms
</font>
<font color="#3A3966">BindGadgetEvent</font>(0, @OnSpeedButtonEvent()) <font color="#3A3966">; Bind all button &quot;-&quot; events
</font><font color="#3A3966">BindGadgetEvent</font>(1, @OnSpeedButtonEvent()) <font color="#3A3966">; Bind all button &quot;+&quot; events
</font><font color="#3A3966">BindEvent</font>(<font color="#924B72">#PB_Event_Timer</font>, @OnTimer())    <font color="#3A3966">; Bind the timer event
</font>
<b><font color="#3A3966">Repeat</font></b> : <b><font color="#3A3966">Until</font></b> <font color="#3A3966">WaitWindowEvent</font>(10) = <font color="#924B72">#PB_Event_CloseWindow</font>
</font></pre>
</body></html>
